* Generates figures for sensitivity analysis
	
sort ccode largeDevaluation year
bys ccode largeDevaluation: gen episodeC = _n
replace episodeC=. if largeDevaluation==.
qui su episodeC
local max_epiC=`r(max)'
forval i=1/`max_epiC'{
gen indicator = 0
replace indicator=1 if episodeC==`i'
gen yearEpiC_temp = indicator * year if episodeC==`i'
bys ccode: egen yearEpiC`i' = total(yearEpiC_temp)
drop indicator yearEpiC_temp
replace yearEpiC`i'=. if yearEpiC`i'==0
gen normyearEpiC`i'= year - yearEpiC`i'
replace normyearEpiC`i'=. if normyearEpiC`i'>`4' | normyearEpiC`i'<`3'
}
sort ccode year
	
* Expand the data to allow multiple episodes by country, create episode identifier and normyear
merge m:1 ccode using "temp/Numb Events by Country.dta"
drop if _merge==1 // countries without any events
drop _merge
levelsof ccode, local(CountryList)
foreach n of local CountryList {
preserve
keep if ccode=="`n'"
local factor = numbepi[1]
restore
expand `factor'	if ccode=="`n'"
}

* Create episode identifier and period (normyear) variable
sort ccode year
bys ccode year: gen episode=_n
merge m:1 ccode episode using "temp/Depreciation Events.dta"
drop _merge
gen normyear=year-devaluationYear		
egen epi = group(ccode episode)

* Growth rates relative to pre deva year 
foreach var in importshare importshare2 rer rerPWT exportshare rer_cpi MIMD MIMD2 e1 SXSD {
gen temp=`var' if normyear==-1
by ccode episode, sort: egen base=max(temp)
gen rel`var'=(`var'/base-1)*100
drop temp base
}

* Adjustments due to transition of data in 1995 from JN to WIOD/OECD // omitting these changes has minimal effects
* For events in 96-00 window, adjust pre-devaluation window: set variables to missing when period is prior to 1995
gen T_temp=0
replace T_temp=1 if largeDevaluation==1  & year<=2000 & year>1995 & importshare!=importshare2 & devaluationYear==year // WIOD/OECD available
bys ccode episode: egen T =total(T_temp)
foreach var in relimportshare2 relrer relrerPWT relexportshare relrer_cpi relMIMD relMIMD2 {
replace `var' =. if normyear>=`3' & normyear<=`4' & year<1995 & T==1
}

* For events in 90-95 window, adjust post-devaluation window: when WIOD/OECD available, set import share and relative spending to JN for periods post 1995 to ensure continuity
gen T_temp2=0
replace T_temp2=1 if largeDevaluation==1  & year<=1995 & year>=1990 & devaluationYear==year // WIOD/OECD available
bys ccode episode: egen T2 =total(T_temp2)
replace relimportshare2 =relimportshare if normyear>=`3' & normyear<=`4' & year>=1995 & T2==1 & importshare!=.
replace relMIMD2 =relMIMD if normyear>=`3' & normyear<=`4' & year>=1995 & T2==1 & importshare!=.

* Adjust data for overlapping events
qui do "Analysis_dofiles/Overlap.do"
	
* Focus on window around events
keep if normyear>=`3' & normyear<=`4'
	
* Average Figures: series of collapses begin now
set graphics on
preserve
	collapse (mean) relimportshare relimportshare2 relrer relrerPWT rele1 relexportshare relrer_cpi relMIMD, by(normyear)
	label var normyear "Year Since Devaluation"
	label var relrer_cpi "IFS CPI RER"
	label var relrer "IFS RER"
	label var relrerPWT "PWT RER"
	label var relimportshare "Imported Input Share Growth"
	label var relimportshare2 "Imported Input Share Growth"
	label var relexportshare "Export Share Growth"
	save "temp/F2_means.dta", replace
restore
preserve
	collapse (semean) relimportshare relimportshare2 relrer relrerPWT rele1  relexportshare relrer_cpi, by(normyear) 
	foreach var in relimportshare relimportshare2 relrer relrerPWT  relexportshare relrer_cpi { 
	ren `var' `var'_se
	}	
	save "temp/F2_se.dta", replace
restore
	
*. Put means and SE together for F2 and F4.
preserve
	use "temp/F2_means.dta",clear
	merge 1:1 normyear using "temp/F2_se.dta"
	gen up = relimportshare2 + relimportshare2_se
	gen down = relimportshare2 - relimportshare2_se
	gen up_rer = relrer + relrer_se
	gen down_rer = relrer - relrer_se	
	graph set window fontface "Times"
	tw (line relimportshare2 normyear, lc(blue) lw(thick)) ///
	   (line up normyear, lpattern(shortdash) lwidth(vthin) lc(blue)) (line down normyear, lpattern(shortdash) lwidth(vthin) lc(blue)) ///
	   (scatter relrer normyear, c(l) color(red) ms(X) msize(medlarge) lw(thick)) ///
	   (line up_rer normyear, lpattern(shortdash) lwidth(vthin) lc(red)) (line down_rer normyear, lpattern(shortdash) lwidth(vthin) lc(red)) ///
	  ,yline(0,lc(black)) xline(0,lc(black) lpattern(dash))  xt("Year Since Devaluation") yt("Percent Change") /// 
	  graphregion(margin(l=0 r=0 b=0 t=0)) graphregion(color(white)) ///
	  legend(off) scale(1.2) ///
	  text(-15 -1  "RER", color(red) size(small)) /// 
	  text(10 -1 "Import" "Share", color(blue) size(small)) ///	  
	  xlab(`3'(1)`4') ///
	  title("{bf:`5'}", color(black))
restore
